package it.globalgamejam.scar.game 
{
	import flash.display.BitmapData;
	import flash.events.TextEvent;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	import it.globalgamejam.scar.Assets;
	import it.globalgamejam.scar.components.CustomDraw;
	import it.globalgamejam.scar.game.world.Ball;
	import it.globalgamejam.scar.game.world.Goal;
	import it.globalgamejam.scar.game.world.Player;
	import it.globalgamejam.scar.Main;
	import it.globalgamejam.scar.menu.MenuWorld;
	import net.flashpunk.Entity;
	import net.flashpunk.FP;
	import net.flashpunk.graphics.Canvas;
	import net.flashpunk.graphics.Image;
	import net.flashpunk.graphics.Stamp;
	import net.flashpunk.graphics.Text;
	import net.flashpunk.Sfx;
	import net.flashpunk.tweens.misc.MultiVarTween;
	import net.flashpunk.tweens.misc.VarTween;
	import net.flashpunk.utils.Draw;
	import net.flashpunk.utils.Input;
	import net.flashpunk.utils.Key;
	import net.flashpunk.World;
	
	/**
	 * ...
	 * @author N1xx1
	 */
	public class GameWorld extends World
	{
		public var isPlaying:Boolean = false;
		
		public static var levels:Array = [Puzzle.puzzle1, Puzzle.puzzle2, Puzzle.puzzle3, Puzzle.puzzle4];
		public static var currentLevel:int = 0;

		public var sfxRoll1:Sfx = new Sfx(Assets.SOUND_BALL_ROLLING_1);
		public var sfxRoll2:Sfx = new Sfx(Assets.SOUND_BALL_ROLLING_2);
		
		public function GameWorld(level:Puzzle) 
		{
			var puzzle:Puzzle = level;
			var player:Player = puzzle.fillWorld(this);
			
			camera = new Point();
			camera.x = player.x - FP.halfWidth;
			camera.y = player.y - FP.halfHeight;
			
			var e:Entity = new Entity(0, 0);
			var canvas:Canvas = new Canvas(FP.width, FP.height);
			canvas.fill(new Rectangle(0, 0, FP.width, FP.height));
			e.graphic = canvas;
			e.graphic.scrollX = e.graphic.scrollY = 0;
			add(e);
			
			var tween5:VarTween = new VarTween(function ():void {
				isPlaying = true;
				remove(e);
			});
			tween5.tween(canvas, "alpha", 0, 1);
			addTween(tween5);
		}
		
		public function endLevel():void {
			sfxRoll1.stop();
			sfxRoll2.stop();
			isPlaying = false;
		}
		
		public function levelLost():void {
			endLevel();
			var e:Entity = new Entity(0, 0);
			var canvas:Canvas = new Canvas(FP.width, FP.height);
			canvas.fill(new Rectangle(0, 0, FP.width, FP.height));
			canvas.alpha = 0;
			e.graphic = canvas;
			e.graphic.scrollX = e.graphic.scrollY = 0;
			add(e);
			
			var tween:VarTween = new VarTween(function ():void {
				FP.world = new GameWorld(levels[currentLevel]);
			});
			tween.tween(canvas, "alpha", 1, 1);
			addTween(tween);
		}
		
		public function nextLevel():void {
			endLevel();
			var ball:Ball = getInstance("ball") as Ball;
			var goal:Goal = getInstance("goal") as Goal;
			
			var tween1:MultiVarTween = new MultiVarTween(function():void {
				var tween2:MultiVarTween = new MultiVarTween(function():void {
					var player:Player = getInstance("player") as Player;
					var tween3:MultiVarTween = new MultiVarTween(function():void {
						var tween4:MultiVarTween = new MultiVarTween(function():void {
							var e:Entity = new Entity(0, 0);
							var canvas:Canvas = new Canvas(FP.width, FP.height);
							canvas.fill(new Rectangle(0, 0, FP.width, FP.height));
							canvas.alpha = 0;
							e.graphic = canvas;
							e.graphic.scrollX = e.graphic.scrollY = 0;
							add(e);
							
							
							var tween5:VarTween = new VarTween(function():void {
								if (currentLevel == levels.length - 1) {
									Main.instance.enterMenu();
									FP.world = new MenuWorld();
								} else {
									FP.world = new GameWorld(levels[++currentLevel]);
								}
							});
							tween5.tween(canvas, "alpha", 1, 1.5);
							addTween(tween5);
							
							var tween6:MultiVarTween = new MultiVarTween();
							tween6.tween(FP.camera, {
								"x": (goal.x + 60 - FP.halfWidth),
								"y": (goal.y + 60 - FP.halfHeight)
							}, 1);
							addTween(tween6);
							
						});
						tween4.tween(Image(player.graphic), {
							"alpha": 0,
							"scale": 0,
							"angle": 3600
						}, 0.3);
						addTween(tween4);
					});
					tween3.tween(player, {
						"x": goal.x + player.width,
						"y": goal.y + player.height
					}, 0.2);
					addTween(tween3);
				});
				tween2.tween(Image(ball.graphic), {
					"alpha": 0,
					"scale": 0
				}, 0.5);
				addTween(tween2);
			});
			tween1.tween(ball, {
				"x": goal.x + 10,
				"y": goal.y + 10
			}, 0.5);
			
			addTween(tween1);
		}
		
		public override function update():void
		{
			super.update();
			
			if(isPlaying) {
				var player:Player = getInstance("player") as Player;
				if (player) {
					camera.x = player.x - FP.width/2;
					camera.y = player.y - FP.height/2;
				}
				
				if (Input.check(Key.R)) {
					levelLost();
				}
			}
		}
		
		override public function render():void
		{
			super.render();
			
			var o:Array = new Array();
			getAll(o);
			for each (var e:Entity in o) {
				Draw.hitbox(e, true, 0xFFFFFF, 1);
			}
			
			var _o:CustomDraw = getInstance("customDraw") as CustomDraw;
			if (_o) {
				Draw.graphic(_o.customGraphic);
			}
		}
	}

}